/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.verticle;

import cn.sunny360.core.common.MessageInterceptorChain;
import cn.sunny360.core.common.MyProperty;
import cn.sunny360.core.common.WebSocketHandler;
import cn.sunny360.core.interceptor.FirstMessageInterceptor;
import cn.sunny360.messsage.factory.MsgDeviceFactory;
import cn.sunny360.messsage.factory.MsgMessageFactory;
import cn.sunny360.messsage.factory.MsgRequestFactory;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;

import java.util.Properties;


/**
 * websocket连接处理Verticle。
 *
 * @author sublun
 */
public class ChatVerticle extends BaseVerticle {

	private static final Logger logger = LoggerFactory.getLogger(ChatVerticle.class);

	private WebSocketHandler webSocketHandler;

	@Override
	public void start() throws Exception {
		//初始化WebSocketServer
		initWebSocketServer();
	}

	private void initWebSocketServer() {
		webSocketHandler = new WebSocketHandler(vertx);

		// get webSocket host and port from configuration, or use default value
		String host = MyProperty.USER_WS_ADDRESS;
		int port = MyProperty.USER_WS_PORT;
		String chatUrl = MyProperty.WS_CHAT_URL;
		webSocketHandler.setCHAT_URL(chatUrl);
		MsgDeviceFactory.protocol=MyProperty.WS_PROTOCOL;
		MsgMessageFactory.protocol=MyProperty.WS_PROTOCOL;
		MsgRequestFactory.protocol=MyProperty.WS_PROTOCOL;
		MsgRequestFactory.protocol=MyProperty.WS_PROTOCOL;

		logger.info("WebSocket server protocol is "+MsgDeviceFactory.protocol);
		wrappingInterceptor(webSocketHandler);
		// create webSocket server
		Promise<HttpServer> promise = Promise.promise();
		vertx.createHttpServer()
				.webSocketHandler(webSocketHandler::webSocketHandler)
				.listen(port, host, promise);
		promise.future()
				.onSuccess(httpServer->{
					logger.info("WebSocket server start success.");
				})
				.onFailure(e->{
					logger.error("WebSocket server create error!", e);
				});
	}

	private void wrappingInterceptor(WebSocketHandler webSocketHandler) {
		MessageInterceptorChain messageInterceptorChain = MessageInterceptorChain.createNew();
		messageInterceptorChain.add(new FirstMessageInterceptor());
		webSocketHandler.setMessageInterceptorChain(messageInterceptorChain);
	}
}
